cc.Class({
  extends: cc.Component,

  properties: {},

  onLoad () {
    this.baseDir = 'right'
  },

  init (setting, tip) {
    this.node._carType = 'train'
    console.log('Train-init')
    this._setting = setting
    this.tip = tip
    this.tip.active = false
    this.setIniPosition()

    setTimeout(()=>{
      this.move()
    },1000 + Math.random() * 2000)

  },

  setIniPosition () {
    console.log('Train-setIniPosition')
    this.node.active = false // 先设active为false,是因为刚体在激活的情况下无法直接修改节点的x、y
    this.node.x = this._setting.x
    this.node.y = this._setting.y
    this.node.rotation = this._setting.rota
    this.node.active = true
    console.log(this.node.x, this.node.y)
  },

  move () {
    console.log('Train-move')
    let tipAction = () => {
      console.log('Train-move-tipAction')
      this.tip.active = true
      console.log(this.tip.x, this.tip.y)
      this.tip.runAction(
        cc.sequence(
          cc.fadeTo(0.3, 255),
          cc.fadeTo(0.3, 0),
          cc.fadeTo(0.3, 255),
          cc.fadeTo(0.3, 0),
          cc.fadeTo(0.3, 255),
          cc.fadeTo(0.3, 0),
          cc.callFunc(nodeAction)
        )
      )
    }

    let nodeAction = () => {
      console.log('Train-move-nodeAction')
      this.setIniPosition()
      let delay = 1 + Math.random() * 2
      this.node.runAction(
        cc.sequence(
          this._setting.action,
          cc.delayTime(delay),
          cc.callFunc(tipAction)
        )
      )
    }
    tipAction()
  },

  stop () {
    this.node.stopAllActions()
    this.tip.stopAllActions()
    this.node.removeFromParent()
    this.tip.removeFromParent()
  },

  start () {},

  update (dt) {
  }
})
